/**   
* @Package com.app.open.sys.user.web 
*/
package com.wlyy.bcwlw.sys.user.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.common.annotation.SysLog;
import com.wlyy.bcwlw.sys.SecurityFacade;
import com.wlyy.bcwlw.sys.application.service.ApplicationService;
import com.wlyy.bcwlw.sys.user.UserConstant;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.entity.UserDTO;
import com.wlyy.bcwlw.sys.user.service.UserManageService;

/** 
 * <p>Description: TODO</p>
 * @author zhangyang
 * <p>日期:2015年6月11日 上午9:24:34</p> 
 * @version V1.0 
 */
@RemoteProxy
@Controller
@RequestMapping("/sys/user")
public class SysUserController extends BaseController{
	
	@Autowired
	private ApplicationService applicationService;
	@Autowired
	private UserManageService userService;
	@Autowired
	private UserDAO userDAO;
	
	@RequestMapping("/index")
	public String userIndex(HttpServletRequest request, HttpServletResponse response){
		return "sys/user/user-index";
	}
	
	@RequestMapping("/listing")
	public String list(HttpServletRequest request,HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		String orderBy = get(request,"orderBy");
		String blockUser = get(request,"blockUser");
		String[] ids = StringUtils.split(request.getParameter("ids"), ",");
		List<UserDTO> list = userDAO.findUserList(ids, blockUser, orderBy);
		
		processJsonData4Grid(request, response, limit, curPage, list.size(), list);
		return null;
	}
	
	@RequestMapping("/userlist")
	public String userList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		
		int count = userDAO.count(null);
		
		List<UserDTO> userList = userDAO.queryByObject(null);
		
		processJsonData4Grid(request, response, limit, curPage, count, userList);
		
		return null;
	}
	
	@RequestMapping("/blockedlist")
	public String userBlockedList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		UserDTO user = new  UserDTO();
		user.setBlocked(UserConstant.USER_BLOCKED_YES);
		int count = userDAO.count(user);
		
		List<UserDTO> userList = userDAO.queryByObject(user);
		
		processJsonData4Grid(request, response, limit, curPage, count, userList);
		
		return null;
	}
	
	@RequestMapping("/edit")
	public String userAdd(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userId= get(request,"userId");
		if(StringUtils.isNotEmpty(userId)){
			UserDTO userInfo = userDAO.findById(userId);
			set(request,"userInfo",userInfo);
			set(request,"type","edit");
		}else{
			set(request,"type","add");
		}
		
		return "sys/user/user-edit";
	}
	
	@RequestMapping("/test")
	public String test(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		return "oa/task/test";
	}
	
	@RequestMapping("/viewMyInfo")
	public String viewMyInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userId = SecurityFacade.getCurUserId();
		UserDTO userInfo = userDAO.findByUserId(userId);
		set(request,"userInfo",userInfo);
		return "sys/user/user-my-info";
	}
	
	@RequestMapping("/changeMyPwd")
	public String changeMyPwd(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userId = SecurityFacade.getCurUserId();
		set(request,"userId",userId);
		return "sys/user/user-change-pwd";
	}
	
	@SysLog(description = "更改个人密码")
	public String changeMyPassword(String oldPassword,String newPassword){
		return userService.changeMyPassword(oldPassword,newPassword);
	}
	
	@SysLog(description = "更改个人信息")
	public String changeMyInfo(UserDTO userDTO) {
		return  userService.changeMyInfo(userDTO);
	}
	
	
	public UserDTO saveUserInfo(UserDTO userDTO){
		return userService.saveUserInfo(userDTO);
	}
	
	public boolean saveUserOwnRoles(String userId,String[] ownRoles){
		return userService.saveUserOwnRoles(userId, ownRoles);
	}
	
	public Map<String,Object> showUserOwnRoles(String userId){
		return userService.showUserOwnRoles(userId);
	}
	
	public Map<String,Object> showUserOwnArea(String userId){
		return userService.showUserOwnArea(userId);
	}
	
	public boolean removeUserByUserIds(String[] userIds){
		return userService.removeUserByUserIds(userIds);
	}
	
	public boolean unBlockedUpdateByUserId(String userId){
		UserDTO user = new UserDTO();
		user.setUserId(userId);
		user.setBlocked(UserConstant.USER_BLOCKED_NO);
		userDAO.update(user);
		return true;
	}
	
	
	

}
